package com.zbbt.sportslottery.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gc.materialdesign.views.ProgressBarCircularIndetermininate;
import com.umeng.socialize.sso.UMSsoHandler;
import com.zbbt.sportslottery.R;
import com.zbbt.sportslottery.app.App;
import com.zbbt.sportslottery.app.Constant;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout;
import com.zbbt.sportslottery.ui.widget.swipe.SwipeRefreshLayout.OnRefreshListener;
import com.zbbt.sportslottery.util.KeyUtil;
import com.zbbt.sportslottery.util.LogUtil;
import com.zbbt.sportslottery.util.ToastUtil;
import com.zbbt.sportslottery.util.UmengShare;
import com.zbbt.sportslottery.util.UmengUtil;

/**
 * 
 * @ClassName: ActionInfoActivity
 * @Description: 活动详情
 * @author：LiZhimin
 * @date：2015-3-10 下午5:13:55
 * @version V1.0
 */
public class ActionInfoActivity extends SimpleBaseActivity {

	private WebView webView;
	private TextView tv_right;
	private SwipeRefreshLayout mSwipeRefreshLayout;
	private GestureDetector mGestureDetector;
	private ProgressBarCircularIndetermininate mProgressBar;
	private LinearLayout error_layout;// 无网络时 显示的界面
	private LinearLayout no_data_laLayout;// 没有数据显示的界面
	private boolean isFirst = true;
	private boolean isError = false;
	private String url;
	private int openNum;
	private UmengShare umengShare;
	private String titleText;
	private String imageUrl;
	private String shareTitle;
	private String summaryTx;
	private boolean showShare;

	@Override
	protected void onActivityCreated(Bundle savedInstanceState) {
		setContentView(R.layout.activity_action_info);
		tv_right = (TextView) findViewById(R.id.tv_right);
		Intent intent = getIntent();
		url = intent.getStringExtra("Url");
		LogUtil.printD("url", url);
		titleText = intent.getStringExtra("Title");
		showShare = intent.getBooleanExtra("ShowShare", true);
		imageUrl = intent.getStringExtra("ImageUrl");
		shareTitle = intent.getStringExtra("ShareTitle");
		summaryTx = intent.getStringExtra("SummaryTx");
		openNum = intent.getIntExtra("isOpen", 1);
		umengShare = new UmengShare(this);
		LogUtil.printD("urll", url);
	}

	@SuppressLint({ "SetJavaScriptEnabled", "NewApi" })
	@Override
	protected void initView() {
		no_data_laLayout = (LinearLayout) findViewById(R.id.no_data_layout);
		error_layout = (LinearLayout) findViewById(R.id.error_layout);
		mProgressBar = (ProgressBarCircularIndetermininate) findViewById(R.id.mProgressBar);
		mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
		webView = (WebView) findViewById(R.id.game_data_webview);
		WebSettings settings = webView.getSettings();
		settings.setJavaScriptEnabled(true);
		// 加载需要显示的网页
		if (openNum == 1) {

			LogUtil.printD("aasadsdsaaa", "有数据");
			webView.loadUrl(url);
			webView.setWebViewClient(new WebViewClient() {
				@Override
				public void onPageStarted(WebView view, String url, Bitmap favicon) {
					if (isFirst) {
						mProgressBar.setVisibility(View.VISIBLE);
						// imageView.setVisibility(View.GONE);
					} else {
						mSwipeRefreshLayout.setRefreshing(true);
					}
					isFirst = false;
					super.onPageStarted(view, url, favicon);
				}

				@Override
				public void onPageFinished(WebView view, String url) {
					if (isError) {
						error_layout.setVisibility(View.VISIBLE);
						mSwipeRefreshLayout.setVisibility(View.GONE);
					} else {
						error_layout.setVisibility(View.GONE);
						mSwipeRefreshLayout.setVisibility(View.VISIBLE);
					}
					mSwipeRefreshLayout.setRefreshing(false);
					mProgressBar.setVisibility(View.GONE);
				}

				@Override
				public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
					view.loadUrl("javascript:document.body.innerHTML=\"" + "" + "\"");
					ToastUtil.showShort("网络连接失败,请稍后再试！");
					super.onReceivedError(view, errorCode, description, failingUrl);
					isError = true;
				}

				@Override
				public boolean shouldOverrideUrlLoading(WebView view, String url) {
					view.loadUrl(url); // 在当前的webview中跳转到新的url
					return true;
				}

			});

			mGestureDetector = new GestureDetector(this, mOnGestureListener);

			mSwipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
				@Override
				public void onRefresh() {
					isError = false;
					webView.reload();
				}
			});
		} else if (openNum == 2) {
			LogUtil.printD("aaaaa", "没有数据");
			mSwipeRefreshLayout.setRefreshing(false);
			mProgressBar.setVisibility(View.GONE);
			no_data_laLayout.setVisibility(View.VISIBLE);
		}
		umengShare = new UmengShare(this);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {

		try {
			mGestureDetector.onTouchEvent(ev);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return super.dispatchTouchEvent(ev);
	}

	private OnGestureListener mOnGestureListener = new OnGestureListener() {

		@Override
		public boolean onDown(MotionEvent e) {
			return false;
		}

		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			if ((e2.getX() - e1.getX() > Constant.FLING_MIN_DISTANCE_X ? true : false) && (Math.abs(e2.getY() - e1.getY()) < Constant.FLING_MIN_DISTANCE ? true : false)
					&& (velocityX > Constant.FLING_MIN_VELOCITY)) {
				finish();
				overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
				return true;
			} else {
				return false;
			}

		}

		@Override
		public void onLongPress(MotionEvent e) {

		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
			return false;
		}

		@Override
		public void onShowPress(MotionEvent e) {}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			return false;
		}
	};

	@Override
	protected void setListener() {
		tv_right.setOnClickListener(this);
	}

	@Override
	protected void initData() {

	}

	@Override
	protected void onClickEvent(View v) {
		switch (v.getId()) {
		case R.id.tv_right:
			// if (App.isLogin) {
			// Intent intent = new Intent(context, MainActivity.class);
			// intent.putExtra(KeyUtil.GotoType, KeyUtil.GOTO_FRAGMENT);
			// intent.putExtra(KeyUtil.FragmentKey, KeyUtil.FRAG_USER);
			// startActivity(intent);
			// } else {
			// goActivity(LoginActivity.class);
			// }
			UmengUtil.onEvent(context, Constant.UMENG_HUODONG_SHARE);
			if (url == null)
				url = "";
			if (TextUtils.isEmpty(summaryTx))
				summaryTx = "分享";
			if (shareTitle == null)
				shareTitle = "分享";
			if (imageUrl == null)
				imageUrl = "";
			umengShare.openshare(url, summaryTx, shareTitle, imageUrl, 1);
			break;
		case R.id.error_layout:
			mProgressBar.setVisibility(View.VISIBLE);
			isError = false;
			webView.reload();
			break;
		default:
			break;
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		/** 使用SSO授权必须添加如下代码 */
		UMSsoHandler ssoHandler = UmengShare.mController.getConfig().getSsoHandler(requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
		}
	}

}
